[id].tsx 1.8 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071
  1. /** @jsx h */
  2. /** @jsxFrag Fragment */
  3. import { Fragment, h } from "preact";
  4. import { Head } from "$fresh/runtime.ts";
  5. import { Handlers, PageProps } from "$fresh/server.ts";
  6. import { checkToken } from "utils/server.ts";
  7. import { find } from "utils/db.ts";
  8. import TopBar from "../islands/TopBar.tsx";
  9. import Editor, { EditorMode } from "../islands/Editor.tsx";
  10. interface PostProps {
  11. id: string;
  12. title: string;
  13. content: string;
  14. shared: boolean;
  15. isLogined: boolean;
  16. allowMode: EditorMode;
  17. }
  18. export const handler: Handlers<PostProps> = {
  19. GET(req, ctx) {
  20. const tokenUserId = checkToken(req);
  21. const postId = ctx.params.id;
  22. const post = find(
  23. "Post",
  24. tokenUserId
  25. ? {
  26. id: postId,
  27. user_id: tokenUserId,
  28. }
  29. : { id: postId, shared: true },
  30. ["title", "content", "shared"]
  31. );
  32. if (post.length > 0) {
  33. return ctx.render({
  34. id: postId,
  35. isLogined: Boolean(tokenUserId),
  36. allowMode: tokenUserId ? EditorMode.Both : EditorMode.Read,
  37. title: post[0][0] as string,
  38. content: post[0][1] as string,
  39. shared: post[0][2] as boolean,
  40. });
  41. }
  42. // Redirect to 404 page if not found
  43. return ctx.renderNotFound();
  44. },
  45. };
  46. export default function Post(props: PageProps) {
  47. return (
  48. <>
  49. <Head>
  50. <title>{props.data.title}</title>
  51. </Head>
  52. <div className="pd-page">
  53. <TopBar
  54. id={props.data.id}
  55. title={props.data.title}
  56. shared={props.data.shared}
  57. allowMode={props.data.allowMode}
  58. isLogined={props.data.isLogined}
  59. />
  60. <Editor
  61. id={props.data.id}
  62. content={props.data.content}
  63. allowMode={props.data.allowMode}
  64. />
  65. </div>
  66. </>
  67. );
  68. }